/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ronsicecream.pos.mk.transaction.spk;

import com.buanalintas.bynga.Action;
import com.buanalintas.bynga.Dictionary;
import com.buanalintas.bynga.Field;
import com.buanalintas.bynga.Page;
import com.buanalintas.bynga.Persistence;
import com.buanalintas.bynga.Utility;
import com.ronsicecream.pos.entities.PosTransactionMkWorkOrder;
import com.ronsicecream.pos.mk.transaction.spkdetail.SpkDetail;
import java.util.Calendar;

/**
 *
 * @author user
 */
public class Spk extends Page {

    private Field workOrderId;
    private Field workNumber;
    private Field workOrderDate;
    private Field workOrderStatus;
//    private Field purchaseOrderKeel;
    private Field workOrderTotal;
    private Field workOrderQuantity;
//    private Field purchaseOrderPaid;
    private Field workOrderReceived;
    private Field workOrderUnreceived;
//    private Field purchaseOrderProgress;
    private Field workOrderDescription;
    private Field workOrderTimestamp;
//    private Field workOrderRegionalId;
    private Field workOrderSupplierId;
//    private Field workOrderPosUnitId;
    private Field workOrderUnitId;
    private Field workOrderUserId;

    public Spk() {
        super();

        setTheClass(PosTransactionMkWorkOrder.class);

        setPageDetail(SpkDetail.class);


        workOrderId = new Field();
        workOrderId.setName("workOrderId");
        workOrderId.setCaption("id");
        //purchaseOrderId.setInfo("purchase order id");
        workOrderId.setListProperties(" align=\"left\" nowrap");
        workOrderId.setInputProperties(" size=\"20\"");
        getFields().add(workOrderId);

        workNumber = new Field();
        workNumber.setName("workOrderNumber");
        workNumber.setCaption("number");
        workNumber.setRequired(true);
        workNumber.setListProperties(" align=\"left\" width=\"20%\" nowrap");
        workNumber.setInputProperties(" size=\"30\" readonly");
        getFields().add(workNumber);

        workOrderDate = new Field();
        workOrderDate.setName("workOrderDate");
        workOrderDate.setCaption("date");
        workOrderDate.setRequired(true);
        workOrderDate.setListProperties(" align=\"center\" width=\"10%\" nowrap");
        workOrderDate.setInputProperties(" size=\"8\"");
        workOrderDate.setDefaultValue(Utility.getTimestamp());
        getFields().add(workOrderDate);

//        purchaseOrderStatus = new Field();
//        purchaseOrderStatus.setName("purchaseOrderStatus");
//        purchaseOrderStatus.setCaption("status");
//        //purchaseOrderStatus.setInfo("purchase order status");
//        purchaseOrderStatus.setListProperties(" align=\"left\" width=\"1%\" nowrap");
//        purchaseOrderStatus.setInputProperties(" size=\"20\"");
//        getFields().add(purchaseOrderStatus);

//        purchaseOrderKeel = new Field();
//        purchaseOrderKeel.setName("purchaseOrderKeel");
//        purchaseOrderKeel.setCaption("keel");
//        //purchaseOrderKeel.setInfo("purchase order keel");
//        purchaseOrderKeel.setListProperties(" align=\"left\" width=\"5%\" nowrap");
//        purchaseOrderKeel.getOptions().add(new String[]{"1", " No"});
//        purchaseOrderKeel.getOptions().add(new String[]{"2", " Yes"});
//        getFields().add(purchaseOrderKeel);

        workOrderTotal = new Field();
        workOrderTotal.setName("workOrderTotal");
        workOrderTotal.setCaption("total");
        //purchaseOrderTotal.setInfo("purchase order total");
        workOrderTotal.setListProperties(" align=\"left\" width=\"10%\" nowrap");
        workOrderTotal.setInputProperties(" size=\"20\"");
        getFields().add(workOrderTotal);

        workOrderQuantity = new Field();
        workOrderQuantity.setName("workOrderQuantity");
        workOrderQuantity.setCaption("Quantity");
        //purchaseOrderQuantity.setInfo("purchase order total");
        workOrderQuantity.setListProperties(" align=\"left\" width=\"10%\" nowrap");
        workOrderQuantity.setInputProperties(" size=\"20\"");
        getFields().add(workOrderQuantity);

//        purchaseOrderPaid = new Field();
//        purchaseOrderPaid.setName("purchaseOrderPaid");
//        purchaseOrderPaid.setCaption("paid");
//        //purchaseOrderPaid.setInfo("purchase order paid");
//        purchaseOrderPaid.setListProperties(" align=\"left\" width=\"10%\" nowrap");
//        purchaseOrderPaid.setInputProperties(" size=\"20\"");
//        getFields().add(purchaseOrderPaid);

        workOrderReceived = new Field();
        workOrderReceived.setName("workOrderReceived");
        workOrderReceived.setCaption("received");
        //purchaseOrderReceived.setInfo("purchase order received");
        workOrderReceived.setListProperties(" align=\"left\" width=\"10%\" nowrap");
        workOrderReceived.setInputProperties(" size=\"20\"");
        getFields().add(workOrderReceived);

        workOrderUnreceived = new Field();
        workOrderUnreceived.setName("workOrderUnreceived");
        workOrderUnreceived.setCaption("unreceived");
        //purchaseOrderUnreceived.setInfo("purchase order unreceived");
        workOrderUnreceived.setListProperties(" align=\"left\" width=\"10%\" nowrap");
        workOrderUnreceived.setInputProperties(" size=\"20\"");
        getFields().add(workOrderUnreceived);

//        purchaseOrderProgress = new Field();
//        purchaseOrderProgress.setName("purchaseOrderProgress");
//        purchaseOrderProgress.setCaption("progress");
//        //purchaseOrderProgress.setInfo("purchase order progress");
//        purchaseOrderProgress.setListProperties(" align=\"left\" width=\"10%\" nowrap");
//        purchaseOrderProgress.getOptions().add(new String[]{"1", "NOT ENOUGH"});
//        purchaseOrderProgress.getOptions().add(new String[]{"2", " ENOUGH"});
//        getFields().add(purchaseOrderProgress);

        workOrderDescription = new Field();
        workOrderDescription.setName("workOrderDescription");
        workOrderDescription.setCaption("description");
        //purchaseOrderDescription.setInfo("purchase order description");
        workOrderDescription.setListProperties(" align=\"left\" width=\"30%\" nowrap");
        workOrderDescription.setInputProperties(" cols=\"50\" rows=\"5\"");
        workOrderDescription.setInputType(Field.INPUT_TEXT_AREA);
        workOrderDescription.setInputCase(Field.CASE_UPPER);
        getFields().add(workOrderDescription);

        workOrderTimestamp = new Field();
        workOrderTimestamp.setName("workOrderTimestamp");
        workOrderTimestamp.setCaption("timestamp");
        //purchaseOrderTimestamp.setInfo("purchase order timestamp");
        workOrderTimestamp.setListProperties(" align=\"center\" width=\"10%\" nowrap");
        workOrderTimestamp.setInputProperties(" size=\"12\"");
//        purchaseOrderTimestamp.setFormatter(DateFormatter.getInstanceDateTime(this));
        getFields().add(workOrderTimestamp);

//        workOrderRegionalId = new Field();
//        workOrderRegionalId.setName("workOrderRegionalId");
//        workOrderRegionalId.setCaption("regional");
//        workOrderRegionalId.setRequired(true);
//        workOrderRegionalId.setListProperties(" align=\"left\" width=\"10%\" nowrap");
//        workOrderRegionalId.setInputType(Field.INPUT_COMBO_BOX);
//        getFields().add(workOrderRegionalId);

//        workOrderSupplierId = new Field();
//        workOrderSupplierId.setName("purchaseOrderSupplierId");
//        workOrderSupplierId.setCaption("supplier");
//        workOrderSupplierId.setRequired(true);
//        workOrderSupplierId.setListProperties(" align=\"left\" width=\"15%\" nowrap");
//        workOrderSupplierId.setInputProperties(" size=\"20\"");
//        workOrderSupplierId.getSelectorColumns().add(new String[]{"supplierCode", " Code", "width=\"30%\" nowrap"});
//        workOrderSupplierId.getSelectorColumns().add(new String[]{"supplierName", " Name", "width=\"50%\""});
//        workOrderSupplierId.getSelectorColumns().add(new String[]{"supplierPhone", " Phone", "align=\"center\" width=\"20%\" nowrap"});
//        getFields().add(workOrderSupplierId);

        workOrderUnitId = new Field();
        workOrderUnitId.setName("workOrderUnitId");
        workOrderUnitId.setCaption("Unit");
        //purchaseOrderUnitId.setInfo("pos master supplier");
        workOrderUnitId.setListProperties(" align=\"left\" width=\"1%\" nowrap");
        workOrderUnitId.setInputProperties(" size=\"20\"");
        getFields().add(workOrderUnitId);

//        workOrderPosUnitId = new Field();
//        workOrderPosUnitId.setName("workOrderPosUnitId");
//        workOrderPosUnitId.setCaption("Pos Unit");
//        //purchaseOrderUnitId.setInfo("pos master supplier");
//        workOrderPosUnitId.setListProperties(" align=\"left\" width=\"1%\" nowrap");
//        workOrderPosUnitId.setInputProperties(" size=\"20\"");
//        getFields().add(workOrderPosUnitId);

        workOrderUserId = new Field();
        workOrderUserId.setName("workOrderUserId");
        workOrderUnitId.setCaption("User");
        //purchaseOrderUnitId.setInfo("pos master supplier");
        workOrderUserId.setListProperties(" align=\"left\" width=\"1%\" nowrap");
        workOrderUserId.setInputProperties(" size=\"20\"");
        getFields().add(workOrderUserId);



        fillFields();

        getActions().add(Action.TRANS_ADD);
        getActions().add(Action.TRANS_EDIT);
        getActions().add(Action.TRANS_VIEW);

        getFieldsAdd().clear();
        getFieldsAdd().add(workOrderDate);
        getFieldsAdd().add(workNumber);
        getFieldsAdd().add(workOrderSupplierId);
//        getFieldsAdd().add(purchaseOrderRegionalId);
        getFieldsAdd().add(workOrderDescription);

        getFieldsEdit().clear();
        getFieldsEdit().add(workOrderDate);
        getFieldsEdit().add(workNumber);
//        getFieldsEdit().add(workOrderSupplierId);
//        getFieldsEdit().add(purchaseOrderRegionalId);
        getFieldsEdit().add(workOrderDescription);

        getFieldsList().clear();
        getFieldsList().add(workNumber);
        getFieldsList().add(workOrderDate);
//        getFieldsList().add(workOrderSupplierId);
//        getFieldsList().add(workOrderRegionalId);
        getFieldsList().add(workOrderQuantity);
        getFieldsList().add(workOrderReceived);
        getFieldsList().add(workOrderTotal);
        getFieldsList().add(workOrderDescription);

        getFieldsView().clear();
        getFieldsView().add(workOrderDate);
        getFieldsView().add(workNumber);
//        getFieldsView().add(workOrderSupplierId);
//        getFieldsView().add(workOrderRegionalId);
//        getFieldsView().add(workOrderPosUnitId);
        getFieldsView().add(workOrderQuantity);
        getFieldsView().add(workOrderReceived);
        getFieldsView().add(workOrderUnreceived);
        getFieldsView().add(workOrderTotal);
//        getFieldsView().add(purchaseOrderPaid);
//        getFieldsView().add(purchaseOrderKeel);
//        getFieldsView().add(purchaseOrderProgress);
        getFieldsView().add(workOrderDescription);
        getFieldsView().add(workOrderUserId);
        getFieldsView().add(workOrderUnitId);
        getFieldsView().add(workOrderTimestamp);

        getFieldsAdd().remove(workOrderId);
        getFieldsEdit().remove(workOrderId);
        getFieldsList().remove(workOrderId);
        getFieldsView().remove(workOrderId);

        setExportList(getClass().getResourceAsStream("PurchaseOrderList.jasper"));
        setExport(getClass().getResourceAsStream("PurchaseOrder.jasper"));

//        setController(new Controller(this) {
//            @Override
//            public void beforeInsert(Object data, List list) {
//                PosTransactionOutWorkOrder po = (PosTransactionOutWorkOrder) data;
//                EntityManager em = getEntityManager();
//                po.setWorkOrderNumber(generate());
//                po.setWorkOrderUnitId(getCurrentUnit());
//                po.setWorkOrderUserId(getCurrentUser());
//                po.setWorkOrderTimestamp(Utility.getTimestamp());
//                po.setWorkOrderDate(Utility.getTimestamp());
////                po.setPurchaseOrderStatus("1");
////                po.setPurchaseOrderProgsetPurchaseOrderStatusress("1");
//                PosMasterRegional regional = (PosMasterRegional) em.createQuery("SELECT t FROM PosMasterRegional t "
//                        + " WHERE t.regionalId=1").setHint(Persistence.REFRESH_HINT, true).getSingleResult();
//                System.out.println("Cek Regional " + regional);
//                po.setPurchaseOrderRegionalId(regional);
//                po.setPurchaseOrderReceived(BigDecimal.ZERO);
//
//                PosMasterUnit posunit = (PosMasterUnit) em.createQuery("SELECT t FROM PosMasterUnit t"
//                        + " WHERE t.unitUnitTypeId.unitTypeId=1 "
//                        + " AND t.unitUnitId.unitId=" + getCurrentUnit().getUnitId()
//                        + " AND t.unitRegionalId.regionalId=" + po.getPurchaseOrderRegionalId().getRegionalId())
//                        .setHint(Persistence.REFRESH_HINT, true).getSingleResult();
//                System.out.println("unittt :" + posunit + " unit name: " + posunit.getUnitName());
//                po.setPurchaseOrderPosUnitId(posunit);
//                System.out.println("Regional : " + posunit);
//                BigDecimal test = BigDecimal.ZERO;
//                BigDecimal qty = BigDecimal.ZERO;
//                for (int i = 0; i < list.size(); i++) {
//                    List<PosTransactionHoPurchaseOrderSupplierDetail> poDetails = list;
//                    poDetails.get(i).setPurchaseOrderDetailSent(BigDecimal.ZERO);
//                    poDetails.get(i).setPurchaseOrderDetailUnsent(poDetails.get(i).getPurchaseOrderDetailQuantity());
//                    test = test.add(poDetails.get(i).getPurchaseOrderDetailQuantity().multiply(poDetails.get(i).getPurchaseOrderDetailPrice()));
//                    qty = qty.add(poDetails.get(i).getPurchaseOrderDetailQuantity());
//                }
//                po.setPurchaseOrderTotal(test);
//                po.setPurchaseOrderUnreceived(qty);
//                po.setPurchaseOrderQuantity(qty);
//                po.setPurchaseOrderTotal(test);
//            }
//
//            @Override
//            public void afterInsert(Object data, List list) {
//                PosTransactionHoPurchaseOrderSupplier po = (PosTransactionHoPurchaseOrderSupplier) data;
//                EntityManager em = getEntityManager();
//                try {
//                    PosMasterUnit posunit = (PosMasterUnit) em.createQuery("SELECT t FROM PosMasterUnit t"
//                            + " WHERE t.unitUnitTypeId.unitTypeId=1 "
//                            + " AND t.unitUnitId.unitId=" + getCurrentUnit().getUnitId()
//                            + " AND t.unitRegionalId.regionalId=" + po.getPurchaseOrderRegionalId().getRegionalId())
//                            .setHint(Persistence.REFRESH_HINT, true).getSingleResult();
//                    System.out.println("unittt :" + posunit + " unit name: " + posunit.getUnitName());
//                    po.setPurchaseOrderPosUnitId(posunit);
//                    System.out.println("Regional : " + posunit);
//                } catch (Exception e) {
//                    System.out.println("Errororrr " + e);
//                }
//            }
//        });
        getController().setOrder(workOrderTimestamp);
        getController().setOrderType("DESC");
    }

    @Override
    public void beforeList() {
        if (!getCurrentUnit().getUnitId().equals(1)) {
            getController().setCondition("t.workOrderUnitId.unitId=" + getCurrentUnit().getUnitId());
        } else {
            return;
        }

    }
    
//    @Override
//    public void afterInit(HttpServletRequest req, HttpServletResponse res) {
//        purchaseOrderRegionalId.setSelectorCondition("t.productId IN (SELECT t1.stockProductProductId.productId FROM PosMasterStockProduct t1 WHERE t1.stockProductUnitId.unitId=1 AND t1.stockProductStatusId.statusId=1 AND t1.stockProductValue > 0) ");
//
//    }
    @Override
    public void beforeAdd() {
        workNumber.setDefaultValue(generate());
    }

    public String generate() {
        String unitCode = (getCurrentUnit()).getUnitCode();
        Calendar cal = Calendar.getInstance();
        String year = "" + cal.get(Calendar.YEAR);
        String month = Utility.intToRoman(cal.get(Calendar.MONTH) + 1);
        try {
            PosTransactionMkWorkOrder po = (PosTransactionMkWorkOrder) getEntityManager().createQuery("SELECT t FROM PosTransactionMkWorkOrder t "
                    + "WHERE t.workOrderNumber LIKE '%/" + unitCode + "/Rons/SPK-Outlet/%/" + year + "' "
                    + "ORDER BY t.workOrderTimestamp DESC").setHint(Persistence.REFRESH_HINT, true).setFirstResult(0).setMaxResults(1).getSingleResult();
            return Utility.lpad(Integer.parseInt(po.getWorkOrderNumber().substring(0, 4)) + 1, 4, "0") + "/" + unitCode + "/Rons/SPK-Outlet/" + month + "/" + year;
        } catch (Exception e) {
        }
        return "0001/" + unitCode + "/SPK-Outlet/" + month + "/" + year;
    }

    @Override
    public void beforeExport() {
        getExportParams().put("title", Dictionary.getValue(this, "Work Order Supplier"));
    }

    @Override
    public void beforeExportMulti() {
        beforeExport();
    }
}
